using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Collections.Generic;
using Pms.Utils;
using Biblioteca.Utils;
using Biblioteca.DataBase;
/*
 * Autor      : IT-Developer1
 * Data       : 02/04/2010 11:03:48
 * Descricao  : 
 * Alteracoes : 
 * 
 */

namespace SERIX
{
    [System.ComponentModel.DataObject(true)]
   public class CProgramas
   {
       private string IDT_PROG ;
       private string IDT_TPO_PROG ;
       private string DAT_INI ;
       private string NOM_PROG ;
       private string DAT_TER ;

	   /// <sumary>
       /// Construtor da classe programas
       /// <sumary>
       public CProgramas()
       {
   
       }

	   /// <sumary>
       /// Construtor da classe Programas
       /// <sumary>
       public CProgramas( string IDT_PROG, string IDT_TPO_PROG, string DAT_INI, string NOM_PROG, string DAT_TER )
       {
          this.IDT_PROG = IDT_PROG;
          this.IDT_TPO_PROG = IDT_TPO_PROG;
          this.DAT_INI = DAT_INI;
          this.NOM_PROG = NOM_PROG;
          this.DAT_TER = DAT_TER;
       }
	   
	   #region Propriedades
     	/// <sumary>
        /// Identificador do programa
        /// <sumary>
        public string Idt_prog 
		{
		   get
		   {
		       return this.IDT_PROG;
		   }
		   set
		   {
		       this.IDT_PROG = value;
		   }
		}
     	/// <sumary>
        /// Identificador do tipo de programa
        /// <sumary>
        public string Idt_tpo_prog 
		{
		   get
		   {
		       return this.IDT_TPO_PROG;
		   }
		   set
		   {
		       this.IDT_TPO_PROG = value;
		   }
		}
     	/// <sumary>
        /// Data de inicio do programa
        /// <sumary>
        public string Dat_ini 
		{
		   get
		   {
		       return this.DAT_INI;
		   }
		   set
		   {
		       this.DAT_INI = value;
		   }
		}
     	/// <sumary>
        /// Nome do programa
        /// <sumary>
        public string Nom_prog 
		{
		   get
		   {
		       return this.NOM_PROG;
		   }
		   set
		   {
		       this.NOM_PROG = value;
		   }
		}
     	/// <sumary>
        /// Data de fim do programa
        /// <sumary>
        public string Dat_ter 
		{
		   get
		   {
		       return this.DAT_TER;
		   }
		   set
		   {
		       this.DAT_TER = value;
		   }
		}
	    #endregion Propriedades
	
	    /// <summary>
        /// Inserindo um novo Programas.
        /// </summary>
        public void Inserir(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"insert into Serix.SRX_PROGRAMAS
			                  ( IDT_PROG, IDT_TPO_PROG, DAT_INI, NOM_PROG, DAT_TER )
						   values
						      ( (select max(IDT_PROG) from Serix.SRX_PROGRAMAS)+1
                                ,:IDT_TPO_PROG, :DAT_INI, :NOM_PROG, :DAT_TER )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				//command.Parameters.Add(new OracleParameter("IDT_PROG",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_TPO_PROG",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("DAT_INI",OracleType.DateTime)); 
				command.Parameters.Add(new OracleParameter("NOM_PROG",OracleType.VarChar)); 
				command.Parameters.Add(new OracleParameter("DAT_TER",OracleType.DateTime)); 
				
                //command.Parameters["IDT_PROG"].Value = this.IDT_PROG;
                command.Parameters["IDT_TPO_PROG"].Value = this.IDT_TPO_PROG;
                command.Parameters["DAT_INI"].Value = this.DAT_INI;
                command.Parameters["NOM_PROG"].Value = this.NOM_PROG;
                if (String.IsNullOrEmpty(this.DAT_TER))
                {
                    command.Parameters["DAT_TER"].Value = DBNull.Value;
                }
                else
                {
                    command.Parameters["DAT_TER"].Value = this.DAT_TER;
                }  
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	    /// <summary>
        /// Alterando um Programas.
        /// </summary>
        public void Alterar(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"update Serix.SRX_PROGRAMAS set 
			                   IDT_TPO_PROG = :IDT_TPO_PROG, DAT_INI = :DAT_INI, NOM_PROG = :NOM_PROG, DAT_TER = :DAT_TER
						   where
						       IDT_PROG = :IDT_PROG ";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_PROG",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_TPO_PROG",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("DAT_INI",OracleType.DateTime)); 
				command.Parameters.Add(new OracleParameter("NOM_PROG",OracleType.VarChar)); 
				command.Parameters.Add(new OracleParameter("DAT_TER",OracleType.DateTime)); 
				
                command.Parameters["IDT_PROG"].Value = this.IDT_PROG;
                command.Parameters["IDT_TPO_PROG"].Value = this.IDT_TPO_PROG;
                command.Parameters["DAT_INI"].Value = this.DAT_INI;
                command.Parameters["NOM_PROG"].Value = this.NOM_PROG;
                if (String.IsNullOrEmpty(this.DAT_TER))
                {
                    command.Parameters["DAT_TER"].Value = DBNull.Value;
                }
                else
                {
                    command.Parameters["DAT_TER"].Value = this.DAT_TER;
                }  
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	     /// <summary>
        /// Exclui um  Programas.
        /// </summary>
        public void Excluir(string Login)
        {
            Tabela tab = new Tabela("SRX_PROGRAMAS", BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login,BiblioPMS.Demo));
            tab.adicionarFiltro(new Coluna("IDT_PROG", DbType.Int32, this.IDT_PROG));
            tab.delete();
        }
		
       [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
		static public DataTable ObterTodos()
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"SELECT IDT_PROG, NOM_PROG, TO_CHAR(DAT_INI, 'DD/MM/YYYY') AS DAT_INI, IDT_TPO_PROG 
                            FROM Serix.SRX_PROGRAMAS 
                            ORDER BY NOM_PROG";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection)); 
                return tbl; 
                
            }
            catch (Exception e)
            {
 
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		
		static public CProgramas PesquisaporCodigo(string idt_prog, string login)
        {
            CProgramas obj;
            string SQL = @"SELECT IDT_PROG, NOM_PROG, TO_CHAR(DAT_INI, 'DD/MM/YYYY') AS DAT_INI, TO_CHAR(DAT_TER, 'DD/MM/YYYY') AS DAT_TER, IDT_TPO_PROG 
                            FROM Serix.SRX_PROGRAMAS 
                            WHERE IDT_PROG = :IDT_PROG";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.AdicionarParametro(":IDT_PROG", DbType.String, idt_prog);
               
				qy.Executar();
                obj = new CProgramas( qy.ObterCampo("IDT_PROG"), qy.ObterCampo("IDT_TPO_PROG"), qy.ObterCampo("DAT_INI"), qy.ObterCampo("NOM_PROG"), qy.ObterCampo("DAT_TER") );
            }                                  
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return new CProgramas();
            }
			finally
            {
                qy.FecharConexao();
            }
            return obj;
        }
		
		static public int ObterNovoIdt(string login)
        {
            string SQL = @"SELECT PKG_BIBSERIX.OBTER_IDT_PROG() IDT From dual ";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.Executar();
				return Convert.ToInt32(qy.ObterCampo("IDT"));
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return-1;
            }
			finally
            {
                qy.FecharConexao();
            }
        }
		
		static public DataTable ObterPorIDT_TPO_PROG(string idt_tpo_prog)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_PROGRAMAS  where IDT_TPO_PROG = :IDT_TPO_PROG order by NOM_PROG ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_TPO_PROG", OracleType.Number));
                command.Parameters["IDT_TPO_PROG"].Value = idt_tpo_prog;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
    }
}	

